package com.ego.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.po.Category;
import com.ego.product.service.CategoryService;
import com.ego.common.util.Result;

/**
 * @program: ego
 * @description 商品三级分类控制器
 * @author KamTang
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，并且以树形结构组装起来
     */
    @GetMapping("/list/tree")
    public Result list() {
        List<Category> entities = categoryService.cascade();
        return Result.ok().put("data", entities);
    }

    @GetMapping("/path/{catId}")
    public Result getCatPath(@PathVariable("catId") Long catId) {
        Long[] categoryPath = categoryService.getCategoryPath(catId);
        Map<String, Object> map = new HashMap<>(1);
        map.put("list", Arrays.asList(categoryPath));
        return Result.ok().put("categoryPath", map);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{catId}")
    public Result get(@PathVariable("catId") Long catId){
		Category category = categoryService.getById(catId);
        return Result.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody Category category){
		categoryService.save(category);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody Category category){
		categoryService.updateCascade(category);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));
        return Result.ok();
    }

}
